package com.koron.device.line.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.CodeTools;
import com.koron.device.bean.Equipment;
import com.koron.device.bean.EquipmentParams;
import com.koron.device.line.bean.po.LineTowerCorrect;
import com.koron.device.line.bean.query.LineTowerCorrectQuery;
import com.koron.device.line.bean.vo.LineTowerCorrectVO;
import com.koron.device.line.mapper.LineTowerCorrectMapper;
import com.koron.device.line.service.LineTowerCorrectService;
import com.koron.device.mapper.EquipmentMapper;
import com.koron.device.service.EquipmentLocationService;
import com.koron.device.service.EquipmentService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 杆塔校准(LineTowerCorrect)表服务实现类
 *
 * @author jzp
 * @since 2023-03-07 15:12:48
 */
@Service
public class LineTowerCorrectServiceImpl implements LineTowerCorrectService {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private EquipmentLocationService equipmentLocationService;

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public LineTowerCorrectVO getById(SessionFactory factory, String id) {
        LineTowerCorrectMapper mapper = factory.getMapper(LineTowerCorrectMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<LineTowerCorrectVO> list(SessionFactory factory, LineTowerCorrectQuery query) {
        LineTowerCorrectMapper mapper = factory.getMapper(LineTowerCorrectMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<LineTowerCorrectVO> page(SessionFactory factory, PageQuery pageQuery, LineTowerCorrectQuery query) {
        pageQuery.prefixOrders("lineTowerCorrect", null);
        return LineTowerCorrectService.super.page(factory, pageQuery, query);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, LineTowerCorrect lineTowerCorrect) {
        if (StrUtil.isEmpty(lineTowerCorrect.getStatus())) {
            lineTowerCorrect.setStatus("DRAFT");
        }
        if (StrUtil.isEmpty(lineTowerCorrect.getCode())) {
            lineTowerCorrect.setCode(CodeTools.getCodeNo("GTJZ-", 3));
        }
        LineTowerCorrectMapper mapper = factory.getMapper(LineTowerCorrectMapper.class);
        //高德经纬度转GPS经纬度
       /* double[] doubles = LatLonUtil.gcj02_To_Gps84(Double.valueOf(lineTowerCorrect.getNewLatitude()), Double.valueOf(lineTowerCorrect.getNewLongitude()));
        lineTowerCorrect.setNewLatitude(String.valueOf(doubles[0]));
        lineTowerCorrect.setNewLongitude(String.valueOf(doubles[1]));*/
        return mapper.insertOrUpdate(lineTowerCorrect) > 0;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, LineTowerCorrect lineTowerCorrect) {
        LineTowerCorrectMapper mapper = factory.getMapper(LineTowerCorrectMapper.class);
        return mapper.update(lineTowerCorrect) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        LineTowerCorrect lineTowerCorrect = new LineTowerCorrect();
        lineTowerCorrect.setId(id);
        lineTowerCorrect.setStatus(status);
        return update(factory, lineTowerCorrect);
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, LineTowerCorrect lineTowerCorrect) {
        // 修改状态
        updateStatus(factory, lineTowerCorrect.getId(), "REVIEWED");

        // 回写设备名称
        if (!Objects.equals(lineTowerCorrect.getTowerName(), lineTowerCorrect.getNewTowerName())) {
            EquipmentMapper equipmentMapper = factory.getMapper(EquipmentMapper.class);
            Equipment equipment = new Equipment();
            equipment.setId(lineTowerCorrect.getTowerId());
            equipment.setName(lineTowerCorrect.getNewTowerName());
            equipmentMapper.update(equipment);
        }

        // 回写经纬度
        List<EquipmentParams> equipmentParams = equipmentService.getBatchBaseParams(factory, Collections.singletonList(lineTowerCorrect.getTowerId()));
        EquipmentParams longitude = equipmentParams.stream()
                .filter(e -> "经度".equals(e.getParamName()))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("该设备[" + lineTowerCorrect.getTowerName() +"]没有经度参数"));
        EquipmentParams latitude = equipmentParams.stream()
                .filter(e -> "纬度".equals(e.getParamName()))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("该设备[" + lineTowerCorrect.getTowerName() +"]没有纬度参数"));
        LineTowerCorrectMapper lineTowerCorrectMapper = factory.getMapper(LineTowerCorrectMapper.class);
        lineTowerCorrectMapper.updateParam(longitude.getId(), lineTowerCorrect.getNewLongitude());
        lineTowerCorrectMapper.updateParam(latitude.getId(), lineTowerCorrect.getNewLatitude());

        // 考虑修改位置台账的经纬度信息

        return true;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LineTowerCorrectMapper mapper = factory.getMapper(LineTowerCorrectMapper.class);
        return mapper.hidden(id) > 0;
    }
}
